package com.ccb.log;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * 平台日志类，可以将操作日志与服务日志进行分离
 * 
 * @author zhao_zg
 * 
 */
public class PlatformLogger {

	private static final Logger PLATFORM_LOGGER = Logger
			.getLogger(PlatformLogger.class);
	private static final Logger OPERATE_LOGGER = Logger
			.getLogger("operateLogger");
	private static final Logger SERVICE_LOGGER = Logger
			.getLogger("serviceLogger");

	public static final int OFF = Level.OFF_INT;
	public static final int FATAL = Level.FATAL_INT;
	public static final int ERROR = Level.ERROR_INT;
	public static final int WARN = Level.WARN_INT;
	public static final int INFO = Level.INFO_INT;
	public static final int DEBUG = Level.DEBUG_INT;
	public static final int TRACE = Level.TRACE_INT;
	public static final int ALL = Level.ALL_INT;

	private static final String callerFQCN = PlatformLogger.class.getName();

	public static final int LOGGER_TYPE_PLATFORM = 0x1;
	public static final int LOGGER_TYPE_OPERATE = LOGGER_TYPE_PLATFORM << 1;
	public static final int LOGGER_TYPE_SERVICE = LOGGER_TYPE_OPERATE << 1;

	public static Logger getLogger(int loggerType) {
		switch (loggerType) {
		case LOGGER_TYPE_PLATFORM:
			return PLATFORM_LOGGER;
		case LOGGER_TYPE_OPERATE:
			return OPERATE_LOGGER;
		case LOGGER_TYPE_SERVICE:
			return SERVICE_LOGGER;
		default:
			return PLATFORM_LOGGER;
		}
	}

	public static boolean isLoggerOn(int loggerType, int level) {
		return getLogger(loggerType).isEnabledFor(Level.toLevel(level));
	}

	/**
	 * 通过自定义type记录日志
	 * 
	 * @param level
	 *            日志等级
	 * @param type
	 *            日志类型
	 * @param msg
	 *            日志信息
	 * @param throwable
	 *            日志异常
	 */
	public static void log(int level, int type, String msg, Throwable throwable) {
		if ((type & LOGGER_TYPE_PLATFORM) != 0) {
			getLogger(LOGGER_TYPE_PLATFORM).log(callerFQCN,
					Level.toLevel(level), msg, throwable);
		}
		if ((type & LOGGER_TYPE_OPERATE) != 0) {
			getLogger(LOGGER_TYPE_OPERATE).log(callerFQCN,
					Level.toLevel(level), msg, throwable);
		}
		if ((type & LOGGER_TYPE_SERVICE) != 0) {
			getLogger(LOGGER_TYPE_SERVICE).log(callerFQCN,
					Level.toLevel(level), msg, throwable);
		}
	}

	/**
	 * 记录平台日志
	 * 
	 * @param level
	 *            日志等级
	 * @param type
	 *            日志类型
	 * @param msg
	 *            日志信息
	 * @param throwable
	 *            日志异常
	 */
	public static void logPlatformLog(int level, String msg, Throwable throwable) {
	
		log(level, LOGGER_TYPE_PLATFORM, msg, throwable);
	}

	/**
	 * 记录服务日志
	 * 
	 * @param level
	 *            日志等级
	 * @param type
	 *            日志类型
	 * @param msg
	 *            日志信息
	 * @param throwable
	 *            日志异常
	 */
	public static void logServiceLog(int level, String msg, Throwable throwable) {
		//System.out.println("================="+level);
		log(level, LOGGER_TYPE_SERVICE, msg, throwable);
	}

	/**
	 * 记录操作日志
	 * 
	 * @param level
	 *            日志等级
	 * @param type
	 *            日志类型
	 * @param msg
	 *            日志信息
	 * @param throwable
	 *            日志异常
	 */
	public static void logOperateLog(int level, String msg, Throwable throwable) {
		log(level, LOGGER_TYPE_OPERATE, msg, throwable);
	}

}
